#####################################
## "Backyard politics in Foreign Aid"
## William Christiansen
## Tobias Heinrich
## Timothy Peterson
#####################################

## File that generates main graph
## for effects and mediation
#################################


## Load estimates
#################
load("output/est_out_med.Rdata")
out_med <- adply(.data=out_med, .margins=c(2, 3, 4, 5),
                 .fun=function(x) data.frame(Q025=quantile(x, .025),
                                             Q500=quantile(x, .500),
                                             Q975=quantile(x, .975)))
out_med$Effect <- factor(out_med$Effect, levels=rev(c("Total effect", "Direct effect", 
                                                  paste0("Indirect effect (M", 1:6, ")"))))
out_med$Treatment2 <- ifelse(out_med$Treatment == "T1", "Local, no Senator mentioned", 
                             "Local, Senator mentioned")
out_med$DV2 <- ifelse(out_med$DV == "Rating", "Project", "Senator")
out_med$Effect2 <- as.character(out_med$Effect)
out_med$Effect2[out_med$Effect == "Indirect effect (M1)"] <- "Indirect effect:\nBenefits friends, family"
out_med$Effect2[out_med$Effect == "Indirect effect (M2)"] <- "Indirect effect:\nBenefits state"
out_med$Effect2[out_med$Effect == "Indirect effect (M3)"] <- "Indirect effect:\nBenefits U.S. economy"
out_med$Effect2[out_med$Effect == "Indirect effect (M4)"] <- "Indirect effect:\nBenefits U.S. security"
out_med$Effect2[out_med$Effect == "Indirect effect (M5)"] <- "Indirect effect:\nBenefits elites more"
out_med$Effect2[out_med$Effect == "Indirect effect (M6)"] <- "Indirect effect:\nMorality"
out_med$Effect2 <- factor(out_med$Effect2, levels=rev(c("Total effect", "Direct effect", "Indirect effect:\nBenefits friends, family",
                                                        "Indirect effect:\nBenefits state", "Indirect effect:\nBenefits U.S. economy",
                                                        "Indirect effect:\nBenefits U.S. security", "Indirect effect:\nBenefits elites more",
                                                        "Indirect effect:\nMorality")))

for(i in 1:2)
{
  g <- ggplot(data=subset(out_med, Scenario == ifelse(i == 1, "Increase", "Cut")),
              aes(x=Effect2, ymin=Q025, y=Q500, ymax=Q975, colour=DV2, group=DV2))
  g <- g + geom_hline(yintercept=0)
  g <- g + scale_color_manual(values=c("black", "gray50"), name="Evaluation")
  g <- g + geom_pointrange(size=.5, position=position_dodge(width=.37)) + theme_bw()
  g <- g + facet_grid(. ~ Treatment2) + coord_flip() + ylab("Change in evaluation of project and senator")
  g <- g + xlab("") + ggtitle(paste0("Treatment and mediation effects for aid ", ifelse(i == 1, "increases", "cuts")))
  g <- g + theme(axis.text = element_text(size=rel(.75)),
                 panel.grid.major.y = element_line(size=0.25, colour="grey80", 
                                                   linetype="dashed"),
                 panel.grid.major.x = element_line(size=0.25, colour="grey80", 
                                                   linetype="dashed"),               
                 panel.grid.minor = element_blank(),
                 axis.ticks = element_blank(),
                 strip.text = element_text(size=rel(1.0), hjust=0, vjust=1),
                 strip.background = element_blank(),
                 plot.title = element_text(size=rel(1.2), face="bold", hjust=0))
  ggsave(paste0("output/R-Effects-", ifelse(i == 1, "Increase", "Cut"), ".pdf"), width=11, height=5.8)
}



